﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5.Students
{
    public partial class Change : Form
    {
        private static Student s;
        public Change()
        {
            InitializeComponent();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox3.Text = StudentForm.selectStr;
            s = WJNDBEntities.dbhelp.Student.Where(o => o.SNo == comboBox3.Text.Trim()).FirstOrDefault();
            tb2.Text = s.SName;
            comboBox1.Text = s.Gender;
            comboBox2.Text = s.CNo;
        }

        private void Change_Load(object sender, EventArgs e)
        {
            comboBox2.DataSource = WJNDBEntities.dbhelp.Student.ToList();
            comboBox3.DataSource = WJNDBEntities.dbhelp.Student.ToList();
            s = WJNDBEntities.dbhelp.Student.Where(o => o.SNo == comboBox3.Text.Trim()).FirstOrDefault();
            tb2.Enabled = false;
            tb2.Text = s.SName;
            comboBox1.Enabled = false;
            comboBox1.Text = s.Gender;
            comboBox2.Enabled = false;
            comboBox2.Text = s.CNo;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox2.Enabled == false)
            {
                MessageBox.Show("请先编辑！！");
            }
            else
            {
                string sid = comboBox3.Text.Trim();
                string sname = tb2.Text.Trim();
                string sex = comboBox1.Text.Trim();
                string cno = comboBox2.Text.Trim();

                try
                {
                    s = WJNDBEntities.dbhelp.Student.Where(o => o.SNo == sid).FirstOrDefault();
                    s.SName = sname; s.Gender = sex; s.CNo = cno;
                    WJNDBEntities.dbhelp.SaveChanges();
                    MessageBox.Show("提交成功！");
                }
                catch (Exception)
                {
                    MessageBox.Show("提交失败！");
                }
                finally
                {
                    this.Close();
                }
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            tb2.Enabled = true;
            comboBox1.Enabled = true;
            comboBox2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
